package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.mapper.SysUserMapper;
import com.team.recruitment_management_system.pojo.dto.auth.ChangePwdByEmailDto;
import com.team.recruitment_management_system.pojo.dto.auth.EmailAndCaptchaDto;
import com.team.recruitment_management_system.pojo.dto.auth.LoginDto;
import com.team.recruitment_management_system.pojo.dto.auth.RegisterDto;
import com.team.recruitment_management_system.pojo.entity.SysUser;
import com.team.recruitment_management_system.pojo.vo.LoginVo;
import com.team.recruitment_management_system.service.AuthService;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.MD5Util;
import com.team.recruitment_management_system.util.result.Result;
import com.team.recruitment_management_system.util.result.ResultCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.team.recruitment_management_system.pojo.constant.EmailConstant.EMAIL_CHANGE_PW_PRIM;
import static com.team.recruitment_management_system.pojo.constant.EmailConstant.EMAIL_LOGIN_PRIM;

@Service
public class AuthServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements AuthService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Result<LoginVo> userRegister(RegisterDto registerDto) {
        if (!Objects.equals(registerDto.getPassword(), registerDto.getResetPassword())) {
            return Result.build(null, 500, "密码不一致");
        }
        //判断账号是否存在
        SysUser user = sysUserMapper.getUserByUsername(registerDto.getUsername());
        if (user != null) {
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        SysUser user1 = new SysUser();
        user1.setUsername(registerDto.getUsername());
        user1.setPassword(MD5Util.encrypt(registerDto.getPassword()));
        this.save(user1);

        SysUser newUser = sysUserMapper.getUserByUsername(registerDto.getUsername());
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(jwtHelper.createToken(newUser.getId()));
        return Result.ok(loginVo, "成功创建新账号！");
    }

    @Override
    public Result<LoginVo> userLogin(LoginDto loginDto) {
        SysUser loginUser = sysUserMapper.getUserByUsername(loginDto.getUsername());
        //账号判断
        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //账号密码判断
        if (!StringUtils.isEmpty(loginDto.getPassword()) && loginUser.getPassword().equals(MD5Util.encrypt(loginDto.getPassword()))) {
            //账号密码正确
            //根据用户唯一标识生成token
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(jwtHelper.createToken(loginUser.getId()));
            return Result.ok(loginVo);
        }
        //密码错误
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }

    @Override
    public Result<LoginVo> userLoginByEmail(EmailAndCaptchaDto emailAndCaptchaDto) {
        //账号查询
        SysUser loginUser = sysUserMapper.getUserByEmail(emailAndCaptchaDto.getEmail());
        //账号判断
        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }
        String captcha = (String) redisTemplate.opsForValue().get(EMAIL_LOGIN_PRIM + emailAndCaptchaDto.getEmail());
        //账号密码判断
        if (captcha != null && !StringUtils.isEmpty(emailAndCaptchaDto.getCaptcha()) && captcha.equalsIgnoreCase(emailAndCaptchaDto.getCaptcha())) {
            //验证码正确
            //根据用户唯一标识生成token
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(jwtHelper.createToken(loginUser.getId()));
            return Result.ok(loginVo);
        }
        //验证码错误
        return Result.build(null, ResultCodeEnum.CAPTCHA_ERROR);
    }

    @Override
    public Result<Void> userChangePwdByEmail(ChangePwdByEmailDto changePwdByEmailDto) {
        //账号查询
        SysUser user = sysUserMapper.getUserByEmail(changePwdByEmailDto.getEmail());
        //账号判断
        if (user == null) {
            return Result.build(null, ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }

        String captcha = (String) redisTemplate.opsForValue().get(EMAIL_CHANGE_PW_PRIM + changePwdByEmailDto.getEmail());
        //判断
        if (captcha != null && !StringUtils.isEmpty(changePwdByEmailDto.getCaptcha()) && captcha.equalsIgnoreCase(changePwdByEmailDto.getCaptcha())) {
            //验证码正确
            // 使用 LambdaUpdateWrapper 修改指定属性
            LambdaUpdateWrapper<SysUser> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(SysUser::getId, user.getId())
                    .set(SysUser::getPassword, MD5Util.encrypt(changePwdByEmailDto.getNewPassword()));
            this.update(lambdaUpdateWrapper); // 执行更新操作
            return Result.ok(null, "修改成功！");
        }
        //验证码错误
        return Result.build(null, ResultCodeEnum.CAPTCHA_ERROR);
    }

}
